package org.example.springmvc1;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/userController")
public class userController {
    @RequestMapping(path = "/login",method = POST)
    public Boolean login(String userName, String password, HttpSession session){
        //先判断 userName 和 password 是否为空
        if(!StringUtils.hasLength(userName) && !StringUtils.hasLength(password)){
            return false;
        }

        //如果不为空
        if("admin".equals(userName) && "admin".equals(password)){
            //这个地方要设置 session 保证登录信息能保存在前端
            session.setAttribute("userName", "admin");
            return true;
        }
        return false;
//        if(userName == null || "".equals(userName)){
//            return false;
//        }
    }

    @RequestMapping(path = "/getLoginUser",method = GET)
    public String getUserName(HttpSession session){
        if(session.getAttribute("userName") != null){
            return (String)session.getAttribute("userName");
        }
        return "";
    }
}
